package com.lhkk.inventoryvehicle.common

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log
import com.lhkk.inventoryvehicle.MainActivity


/**
 * 自启动广播接收
 */
class BootBroadcastReceiver : BroadcastReceiver() {

    override fun onReceive(context: Context?, intent: Intent?) {
        Log.d("XRGPS", "BootReceiver.onReceive: " + intent?.action)
        println("自启动程序即将执行")/*  val mBootIntent = Intent(context, HomeActivity::class.java)
          //下面这句话必须加上才能开机自动运行app的界面
          mBootIntent.action = "android.intent.action.MAIN"
          mBootIntent.addCategory("android.intent.category.LAUNCHER")
          mBootIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
          context?.startActivity(mBootIntent)*/ //接收广播：系统启动完成后运行程序
        if (intent?.action.equals(Intent.ACTION_BOOT_COMPLETED)) {
            val ootStartIntent = Intent(context, MainActivity::class.java)
            ootStartIntent.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK
            context?.startActivity(ootStartIntent)
        } //接收广播：安装更新后，自动启动自己。
        if (intent?.action.equals(Intent.ACTION_PACKAGE_ADDED) || intent?.action.equals(Intent.ACTION_PACKAGE_REPLACED)) {
            val ootStartIntent = Intent(context, MainActivity::class.java)
            ootStartIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            context?.startActivity(ootStartIntent)
        }
    }
}